package com.gy.flink.state;

import com.gy.flink.WordCountWebUI;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

public class WordCount {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        env.setParallelism(3);
        DataStreamSource<String> dataStream = env.socketTextStream("localhost", 9999);

        dataStream.flatMap((line, out) -> {
            String[] words = line.split(",");
            Arrays.stream(words).forEach(z -> out.collect(Tuple2.of(z, 1)));
        })
                .keyBy(0)
                .sum(1).print().setParallelism(1);


        env.execute(WordCountWebUI.class.getSimpleName());
    }

}
